# 模拟一次同花顺登录 并导出浏览器cookie

from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ChromeOptions
import time
import json

from mq.kafka.kafkaUtil import produceMsg, commonProduceMsg
from utils.DateUtil import getTodayStr

driver_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
option = ChromeOptions()
option.add_argument('--headless')
browser = uc.Chrome(driver_executable_path=driver_path)
browser.set_window_size(1366,768)
browser.get('https://upass.10jqka.com.cn/login')
# 访问登录页
flag = True
print("等待登录...")
while flag:
    # 判断完成登录, browser对象是随着页面变化动态变化的（相当于双向绑定） 所以browser.page_source也是变化的
    flag =  "同花顺财经__让投资变得更简单" not in browser.page_source
    print(flag)
    # 如果没有登录 就等待4S 然后继续扫描
    time.sleep(4)

print("已登录，现在为您保存cookie...")
with open('cookie.txt', 'w', encoding='u8') as f:
    json.dump(browser.get_cookies(), f)

# 记录cookie生成时间，发到kafka=> fetch-data-module => 记录过期时间 => 定时任务扫描
commonProduceMsg('prod',"cookie_expire",getTodayStr())

browser.close()
print("cookie保存完成，游览器已自动退出...")
